.data
    suces_str: .asciiz "\nSuccess! Location: "
    fail_str: .asciiz "\nFail!"
    buffer: .space 20
    exit_str: .asciiz "\n#Stop program#"
    str1:  .asciiz "Enter string: "
    str2:  .asciiz "Enter char: "

.text
.globl main
main:
    # a0 = input/result string
    # a1 = input string length
    # a2 = input char
    # a3 = loop donefail

    # initialize
    add $a3, $zero, $zero
    add $t0, $zero, $zero    # i = 0
    # take string
    la $a0, buffer           # load byte address
    li $a1, 20               # length of string
    li $v0, 8                # read string at a0
    syscall

takechar:
    li $v0, 12               # char read to v0
    syscall
    beq $v0, '?', exit       # if (v0 == '?') then exit
    move $a2, $v0            # a2 = char
    
search:
    # t0 = int i
    beq $t0, $a1, donefail   # if (i = length) then donefail
    la $a0, buffer           # else load byte
    add $t2, $a0, $t0        # t2 = &str + offset
    lb $s1, ($t2)            # s1 = *t2
    beq $s1, $a2, sucess     # if (this.char = char) then sucess
    add $t0, $t0, 1          # else i++
    j search

fail:
    la $a0, fail_str
    li $v0, 4
    syscall
    li $v0, 11
    add $a0, $zero, '\n'
    syscall
    j takechar
      
sucess:
    la $a0, suces_str
    li $v0, 4
    syscall
    li $v0, 1
    add $a0, $t0, 1          # print index begin from 1    
    syscall
    li $v0, 11
    add $a0, $zero, '\n'
    syscall
    j takechar

donefail:
    add $a3, $zero, 1        # flag donefail = 1
    add $t0, $zero, $zero    # reset int i = 0
    j fail

exit:
    la $a0, exit_str
    li $v0, 4
    syscall
